package com.sinoyd.itszyw.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.sinoyd.itszyw.Model.MaintainTaskInfo;
import com.sinoyd.itszyw.R;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

/**
 * @author: Sinoyd  --------->作者
 * @description: --------->描述
 * @projectName: ITSZYW  ---------> 项目名称
 * @date: 2017-03-10 ---------> 创建日期
 * @time: 14:43       ---------> 创建时间
 */
public class MaintainTaskAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private List<MaintainTaskInfo> taskArray;


//    //添加数据集合
//    public void addData(ArrayList<MaintainTaskInfo> list) {
//        taskArray.addAll(list);
//        notifyDataSetChanged();
//    }

    public MaintainTaskAdapter(Context context, List<MaintainTaskInfo> list) {
        this.taskArray = list;
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    private class viewHolder {
        @ViewInject(R.id.task_name)
        TextView task_name;
        @ViewInject(R.id.task_type_name)
        TextView task_type_name;
        @ViewInject(R.id.task_state)
        TextView task_state;
        @ViewInject(R.id.task_repairs)
        TextView task_repairs;
        @ViewInject(R.id.issue_date)
        TextView issue_date;
        @ViewInject(R.id.finish_date)
        TextView finish_date;
        @ViewInject(R.id.task_description)
        TextView task_description;
        @ViewInject(R.id.task_number)
        TextView task_number;
        @ViewInject(R.id.resolved)
        RadioButton resolved;
        @ViewInject(R.id.unsolved)
        RadioButton unsolved;

    }

    @Override
    public int getCount() {
        return taskArray == null ? 0 : taskArray.size();
    }

    @Override
    public Object getItem(int i) {
        return taskArray.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        viewHolder cellHolder = null;
        if (view == null) {
            cellHolder = new viewHolder();
            view = mLayoutInflater.inflate(R.layout.listview_maintain_task, null);
            x.view().inject(cellHolder, view);
            view.setTag(cellHolder);

//            cellHolder.solve_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//                @Override
//                public void onCheckedChanged(RadioGroup radioGroup, int i) {
//                    Integer pos = (Integer) radioGroup.getTag();
//                    MaintainTaskInfo task = taskArray.get(pos);
//                    switch (i){
//                        case R.id.resolved:
//                            task.sloved = 0;
//                            break;
//                        case R.id.unsolved:
//                            task.sloved = 1;
//                            break;
//                        default:
//                            task.sloved = -1;
//                            break;
//                    }
//                }
//            });

        } else {
            cellHolder = (viewHolder) view.getTag();
        }

        MaintainTaskInfo taskInfo = taskArray.get(i);
        cellHolder.task_number.setText(i + 1 + "");
        cellHolder.task_name.setText(taskInfo.name);
        cellHolder.task_type_name.setText(taskInfo.typeName);
        cellHolder.task_repairs.setText(taskInfo.repairs);
        cellHolder.issue_date.setText(taskInfo.issueDate);
        cellHolder.finish_date.setText(taskInfo.finishDate);
        cellHolder.task_description.setText(taskInfo.description);
        if (taskInfo.state.equals("2")){
            cellHolder.task_state.setText("已处理");
            cellHolder.task_state.setBackgroundResource(R.drawable.text_bg_gray);
        }else {
            cellHolder.task_state.setText("处理中");
            cellHolder.task_state.setBackgroundResource(R.drawable.text_bg_blue);

        }
        if (taskInfo.sloved.equals("true")){
            cellHolder.resolved.setChecked(true);
        }else {
            cellHolder.unsolved.setChecked(true);
        }


//        cellHolder.solve_group.setTag(new Integer(i));
//      if (taskArray.get(i).sloved != -1){
//            RadioButton r = (RadioButton) cellHolder.solve_group.getChildAt(taskArray.get(i).sloved);
//            r.setChecked(true);
//        }else{
//          cellHolder.solve_group.clearCheck();
//        }
        return view;
    }


}
